package kr.co.doriclassic.chapter17;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

public class CheckBoxEx extends JPanel implements ItemListener{
	private JPanel chkPanel;
	private JPanel imgPanel;
	JCheckBox[] chkbs = new JCheckBox[3];
	JLabel[] labels = new JLabel[3];
	String[] strs = {"img_1", "img_2", "img_3"};
	ImageIcon[] icons = new ImageIcon[3];
	
	public CheckBoxEx() {
		initialize();
	}
	private void initialize() {
		chkPanel = new JPanel();
		imgPanel = new JPanel();
		for (int i = 0; i < 3; i++) {
			chkbs[i] = new JCheckBox(strs[i]);
			chkbs[i].addItemListener(this);
			labels[i] = new JLabel(strs[i]+".jpg");		
			icons[i] = new ImageIcon(strs[i]+".jpg");
			
			chkPanel.add(chkbs[i]);
			imgPanel.add(labels[i]);
		}
		setLayout(new BorderLayout(0, 0));
		Border border = BorderFactory.createTitledBorder("선택");
		chkPanel.setBorder(border);
		
		add(chkPanel, BorderLayout.WEST);
		chkPanel.setLayout(new BoxLayout(chkPanel, BoxLayout.Y_AXIS));
		add(imgPanel, BorderLayout.CENTER);
		imgPanel.setLayout(new GridLayout(1, 3, 0, 0));
		
	}
	
	@Override
	public void itemStateChanged(ItemEvent e) {
		ImageIcon image=null;
		Object source = e.getItemSelectable();
		for (int i = 0; i < 3; i++) {
			if(source == chkbs[i]){
				if(e.getStateChange()==ItemEvent.DESELECTED){
					labels[i].setIcon(null);
				}
				else{
					labels[i].setIcon(icons[i]);
				}
			}			
		}		
	}
}